
const db = wx.cloud.database()
const _ = db.command
Page({
  data: {
    bulletPoint: '\u2022', // 使用 Unicode 编码表示 ● 符号
    name:'',//项目名称
    total: 13000, // 总目标
    unit: 1500, // 进度单元
    completed: 6000, // 已完成
    currentPercent: 0, // 当前百分比
    BZlist: [],
    BZtype:[],
    fileid: '',
    currentIndex: 0, // 当前已加载的元素索引
    pageSize: 20 // 每次加载的元素数量
  },

    //获取该数据标注详情，并跳转至标注审核页
  // bztype为0，代表审核；通过wxml获取fileid（图片文件id）  bztype为1，代表回放
  getphoto(e) {
    var that = this
    console.log(e.currentTarget.dataset.fileid)
    let currentIndex = wx.getStorageSync('currentIndex')
    //修改缓存中chaochalist，currentIndex
    wx.setStorageSync('currentIndex', currentIndex-1)
    const newArray = wx.getStorageSync('chaochalist').filter(item => item !== e.currentTarget.dataset.fileid);
    wx.setStorageSync('chaochalist', newArray)
    wx.redirectTo({
      url: '/pages/choucha/choucha?fileid='+ encodeURIComponent(e.currentTarget.dataset.fileid),
    })
  },

  //获取审核记录
  async sh_list(){
    var that = this
    let currentIndex = wx.getStorageSync('currentIndex')
    const endIndex = currentIndex + that.data.pageSize;
    let {firstTime,endTime} = that.AboutYesterday()
    console.log(firstTime,endTime)
    await db.collection('ShenQiData')
    .where({
      // cc_id:'oHkCd5EKSl3nEteEg2CzYTgO_GmY',
      confirm:'合格',
      sh_time:_.gte(firstTime).and(_.lt(endTime)),
      cc_id:_.exists(false)

    })
    .skip(wx.getStorageSync('currentIndex'))
    .limit(this.data.pageSize)
    .get()
    .then(res=>{
      const newFileids = res.data.map(item => item.fileid)
      that.setData({
        BZlist: that.data.BZlist.concat(newFileids),
        // currentIndex: endIndex
      });
      wx.setStorageSync('chaochalist', that.data.BZlist)
      wx.setStorageSync('currentIndex', endIndex)
      if (res.data.length < this.data.pageSize) {
        // this.setData({ hasMoreData: false });
        console.log('没有更多数据了')
      }
    })
  },

  //获取昨天最后一刻的时间戳endTime
  AboutYesterday(){
    //获取昨天firstTime 00:00:00,endTime 23:59:99的时间戳
    //=====测试时用的是今天的时间戳=======
    let now = new Date();
    const year = now.getFullYear()
    const month = now.getMonth() + 1
    const day = now.getDate()
    const startOfDay = new Date(year+'/'+month+'/'+day+' '+'00:00:00')   
    const firstTime = Math.floor(startOfDay.getTime() / 1000) -1*86400  //day-1
    const endOfDay = new Date(year+'/'+month+'/'+day+' '+'23:59:00')  
    const endTime = Math.floor(endOfDay.getTime() / 1000) - 1*86400 //day-1
    return{
      firstTime,
      endTime
    }
  },

    /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    var that = this
    // 计算当前完成的百分比
    this.setData({
      name:'打电话',
      currentPercent: Math.round((this.data.completed / this.data.total) * 100)
    });
    console.log(options.name)
    if(options.name == 'choucha') {
      that.setData({
        BZlist: wx.getStorageSync('chaochalist')
      })
    }else{
      wx.showLoading({
        title: '加载中',
      })
      // 调用共享环境的数据库worktasks，为BZtype赋值
      var c1 = new wx.cloud.Cloud({
        resourceAppid: 'wxa862ef26dee146f8',
        resourceEnv: 'cytfadmini-1gali3r244372a1f',
      })
      await c1.init()
      var db1 = c1.database()
      db1.collection('worktasks')
      .where({
        rwname:'数据标注-KZ' //当options.name未定义时，它将获取所有记录？
      })
      .get()
      .then(res=>{
        console.log(res)
        //将BZtype存入缓存，key=‘BZtype'
        wx.setStorageSync('BZtype', res.data[0].BZtype)
        that.data.BZtype = res.data[0].BZtype
      })
      // currentIndex 清零
      wx.setStorageSync('currentIndex', 0)
      // 调用审核列表
      await that.sh_list()
      wx.hideLoading()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  async onReachBottom() {
    // this.loadMoreData();
    console.log('到底了')
    this.sh_list()
  },
});